/*
 * @Author: sean_kei@163.com
 * @Date: 2021-06-08 15:07:23
 * @LastEditors: sean_kei@163.com
 * @LastEditTime: 2022-08-24 14:21:57
 */
package com.wxhandle.cleandemo.api.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.wxhandle.cleandemo.api.exception.NotFoundException;
import com.wxhandle.cleandemo.application.dto.identity.SysClaimDto;
import com.wxhandle.cleandemo.application.dto.identity.SysUserDto;
import com.wxhandle.cleandemo.application.dto.identity.SysUserParam;
import com.wxhandle.cleandemo.core.command.identity.RegisterUserCommand;
import com.wxhandle.cleandemo.core.domain.identity.SysUser;
import com.wxhandle.cleandemo.core.domain.identity.SysService.ClaimType;
import com.wxhandle.cleandemo.core.services.SysUserService;

import org.modelmapper.ModelMapper;
import org.springframework.data.domain.Page;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;


@Api(tags="用户接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/user")
public class SysUserController {

    private final SysUserService userService;
    private final ModelMapper modelMapper;

    @ApiOperation("分页查询用户")
    @GetMapping("page")
    public Page<SysUserDto> getUserPage(SysUserParam param, int page,int size) throws NotFoundException{
      if(page>0)
        throw new NotFoundException("没有发现资源");
      Page<SysUser> userPage = userService.getPage(param, page, size);
      return userPage.map(u->modelMapper.map(u, SysUserDto.class));
    }

    @ApiOperation("注册用户")
    @PostMapping
    public SysUserDto registerUser(@RequestBody RegisterUserCommand command) {
      PasswordEncoder encoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
      command.setPwd(encoder.encode(command.getPwd()));
      return modelMapper.map(userService.register(command), SysUserDto.class) ;
    }

    @ApiOperation("我的信息")
    @GetMapping(value="myinfo")
    public SysUserDto getMyInfo(@AuthenticationPrincipal Jwt jwt) {
      if(null == jwt)
        return null;
        // Long id = jwt.getClaim("id");
      return modelMapper.map(userService.getOneByLogin(jwt.getSubject()), SysUserDto.class);
    }

    @ApiOperation("我的菜单")
    @GetMapping(value="mymenu")
    public List<SysClaimDto> getMyMenu(@AuthenticationPrincipal Jwt jwt) {
      if(null == jwt)
        return null;
      return userService.getClaimsById(Long.parseLong(jwt.getId()), ClaimType.MENU).stream()
        .map(m-> modelMapper.map(m, SysClaimDto.class)).collect(Collectors.toList());
    }


    @ApiOperation("我的权限")
    @GetMapping(value="mypermission")
    public List<SysClaimDto> getMyPermssion(@AuthenticationPrincipal Jwt jwt) {
      if(null == jwt)
        return null;
      return userService.getClaimsById(Long.parseLong(jwt.getId()), ClaimType.PERMISSION).stream()
        .map(m-> modelMapper.map(m, SysClaimDto.class)).collect(Collectors.toList());
    }



}
